package com.chenzp.rocketmq.order;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

@Slf4j
public class OrderProducer {

    @SneakyThrows
    public static void main(String[] args) {
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("my-group-producer");
        defaultMQProducer.setNamesrvAddr("192.168.221.100:9876");
        defaultMQProducer.start();
        for (int i = 0; i < 10; i++) {
            int orderId = i;
            for (int j = 0; j < 5; j++) {
                Message message = new Message("orderTopicTest", "order_" + orderId,"key_" + orderId,
                        ("order_"+i+" step" + j).getBytes(StandardCharsets.UTF_8));
                SendResult result = defaultMQProducer.send(message, (mqs, msg, arg) -> {
                    Integer id = (Integer) arg;
                    int index = id % mqs.size();
                    return mqs.get(index);
                }, orderId);
                log.info("顺序消息发送结果{}", result);
            }

        }
        defaultMQProducer.shutdown();
    }
}
